package com.example.a2306_fx.detail

import androidx.lifecycle.viewModelScope
import com.example.base.entity.AddCartEntity
import com.example.base.entity.DetailEntity
import com.example.base.entity.Res
import com.example.base.mvvm.BaseViewModel
import com.example.base.utils.SingleLiveData
import kotlinx.coroutines.launch

class DetailViewModel:BaseViewModel() {
    //获取详情
    val detailSuccess = SingleLiveData<Res<DetailEntity>>()
    val detailFail = SingleLiveData<String>()
    fun detail(id:Int){
        viewModelScope.launch {
            try {
                detailSuccess.postValue(apiService.detail(id,"normal"))
            } catch (e: Exception) {
                detailFail.postValue(e.message.toString())
            }
        }
    }

    val addSuccess = SingleLiveData<Res<AddCartEntity>>()
    val addFail = SingleLiveData<String>()
    fun cartAdd(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["cartNum"] = cartNum
                map["productAttrUnique"] = productAttrUnique
                map["productId"] = productId
                addSuccess.postValue(apiService.cartAdd(mapToBody(map)))
            } catch (e: Exception) {
                addFail.postValue(e.message.toString())
            }
        }
    }
}